package cn.fscode.code.generator.repository;

import cn.fscode.code.generator.mapper.TemplateConfigMapper;
import cn.fscode.code.generator.model.po.TemplateConfigPO;
import cn.fscode.code.generator.model.req.CopyTemplateReq;
import cn.fscode.code.generator.model.req.TemplateQueryReq;
import cn.fscode.commons.core.page.PageDomain;
import cn.fscode.commons.core.page.TableSupport;
import cn.fscode.commons.tool.core.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Objects;

/**
 * @author shenguangyang
 */
@Repository
public class TemplateConfigRepository extends ServiceImpl<TemplateConfigMapper, TemplateConfigPO>
        implements IService<TemplateConfigPO> {
    public Page<TemplateConfigPO> pageTemplateConfig(TemplateQueryReq req) {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();

        LambdaQueryWrapper<TemplateConfigPO> queryWrapper = Wrappers.<TemplateConfigPO>lambdaQuery()
                .eq(Objects.nonNull(req.getGroupId()), TemplateConfigPO::getGroupId, req.getGroupId())
                .like(StringUtils.isNotEmpty(req.getName()), TemplateConfigPO::getName, req.getName())
                .orderByDesc(TemplateConfigPO::getUpdateTime);
        return this.page(new Page<>(pageNum, pageSize), queryWrapper);
    }

    public TemplateConfigPO getTemplateByName(CopyTemplateReq req) {
        LambdaQueryWrapper<TemplateConfigPO> queryWrapper = Wrappers.<TemplateConfigPO>lambdaQuery()
                .eq(Objects.nonNull(req.getSourceGroupId()), TemplateConfigPO::getGroupId, req.getSourceGroupId())
                .eq(StringUtils.isNotEmpty(req.getName()), TemplateConfigPO::getName, req.getName());
        return this.getOne(queryWrapper);
    }

    public List<TemplateConfigPO> listTemplateConfig(TemplateQueryReq req) {
        LambdaQueryWrapper<TemplateConfigPO> queryWrapper = Wrappers.<TemplateConfigPO>lambdaQuery()
                .eq(Objects.nonNull(req.getGroupId()), TemplateConfigPO::getGroupId, req.getGroupId())
                .like(StringUtils.isNotEmpty(req.getName()), TemplateConfigPO::getName, req.getName())
                .orderByDesc(TemplateConfigPO::getUpdateTime);
        return this.list(queryWrapper);
    }

    public TemplateConfigPO getTemplateByNameInGroup(String name, Long groupId) {
        LambdaQueryWrapper<TemplateConfigPO> queryWrapper = Wrappers.<TemplateConfigPO>lambdaQuery()
                .eq(Objects.nonNull(groupId), TemplateConfigPO::getGroupId, groupId)
                .eq(StringUtils.isNotEmpty(name), TemplateConfigPO::getName, name);
        return this.getOne(queryWrapper);
    }

    public void updateByNameAndGroup(TemplateConfigPO req) {
        LambdaQueryWrapper<TemplateConfigPO> queryWrapper = Wrappers.<TemplateConfigPO>lambdaQuery()
                .eq(Objects.nonNull(req.getGroupId()), TemplateConfigPO::getGroupId, req.getGroupId())
                .eq(StringUtils.isNotEmpty(req.getName()), TemplateConfigPO::getName, req.getName());
        this.update(req, queryWrapper);
    }
}
